package com.example.Microblogging.controller;

import com.example.Microblogging.dto.TaskUserAccount;
import com.example.Microblogging.entity.Task;
import com.example.Microblogging.entity.UserAccount;
import com.example.Microblogging.services.TaskService;
import com.example.Microblogging.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("task")
public class TaskController {

    @Autowired
    TaskService taskService;

    @Autowired
    UserService userService;

    @GetMapping
    public String DisplayTasks(Model model){
        List<TaskUserAccount> taskUserAccounts = taskService.taskUserAccounts();
        model.addAttribute("taskUserAccount", taskUserAccounts);
        return "";
    }

    @GetMapping("/new")
    public String DisplayTaskForm(Model model){
        Task aTask=new Task();
        List<UserAccount> userAccounts = userService.getAll();
        model.addAttribute("allUserAccount",userAccounts);
        model.addAttribute("task",aTask);
        return "";
    }

    @PostMapping("/save")
    public String CreateTask(Model model, @RequestParam List<Long> members, Task task){
        taskService.save(task);
        return "";
    }
}
